package com.invengo.module_check.ui.fragment.setting;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.launcher.ARouter;
import com.invengo.base.router.RouterActivityPath;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.FragmentSettingBinding;
import com.invengo.module_check.ui.activity.login.LoginActivity;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;

/**
 * @author buoker
 * @version 1.0.0
 * @date 2023/10/24
 * @className java.lang.String
 */
public class SettingFragment extends BaseFragment<FragmentSettingBinding, SettingViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_setting;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    MaterialDialog conformDialog;

    @Override
    public void initViewObservable() {
        boolean aBoolean = false;
        if (getArguments() != null) {
            aBoolean = getArguments().getBoolean(BundleKey.KEY_FROM_HOME);
        }
        binding.comFeature.setVisibility(aBoolean ? View.GONE : View.VISIBLE);
        binding.comLogout.setVisibility(aBoolean ? View.VISIBLE : View.GONE);
        binding.comConform.setVisibility(!aBoolean ? View.VISIBLE : View.GONE);
        Bundle bundle =new Bundle();
        bundle.putBoolean(BundleKey.KEY_FROM_HOME,aBoolean);
        viewModel.uc.feature.observe(this, view -> NavHostFragment.findNavController(this).navigate(R.id.com_action_com_settingfragment_to_com_featurefragment));
        viewModel.uc.netModel.observe(this, view -> NavHostFragment.findNavController(this).navigate(R.id.com_action_com_settingfragment_to_com_netmodelfragment,bundle));
        viewModel.uc.rfidSetting.observe(this, view -> NavHostFragment.findNavController(this).navigate(R.id.com_action_com_settingfragment_to_com_rfidsettingfragment));
        viewModel.uc.logoutEvent.observe(this, unused -> {
            ARouter.getInstance().build(RouterActivityPath.Check.PAGER_LOGIN).navigation();
        });
        viewModel.uc.conformDialogEvent.observe(this, title -> {
            if (conformDialog == null)
                initConformDialog();
            conformDialog = conformDialog.getBuilder().content("该操作将会" + title + "，请确认...").build();
            if (!conformDialog.isShowing())
                conformDialog.show();
        });
    }

    private void initConformDialog() {
        conformDialog = MaterialDialogUtils.showBasicDialog(getActivity(), "温馨提示！")
                .contentGravity(GravityEnum.CENTER)
                .negativeText("取消")
                .onNegative((dialog, which) -> {
                    dialog.dismiss();
                })
                .positiveText("确定")
                .onPositive((dialog, which) -> {
                    dialog.dismiss();
                    viewModel.conformOpeartion();
                })
                .build();
    }
}
